import Utils from "../game/utils";
import EditGlobal from "./EditGlobal";
import EditTool from "./EditTool";

export default class EditCell extends Laya.Sprite {

    constructor(row, col) {
        super();
        this.row = row;
        this.col = col;
        this.x = Utils.gridToStageX(row);
        this.y = Utils.gridToStageY(col);
        this.pivot(35, 35);
        this.assetNames = ["bonus color", "blue", "green", "purple", "orange", 'cupcake'];

        this.bgmark = new Laya.Sprite();
        this.bgmark.pivot(35, 35);
        this.bgmark.x = Utils.gridToStageX(this.row);
        this.bgmark.y = Utils.gridToStageY(this.col);
        EditGlobal.main.game_bg.addChild(this.bgmark);


        var mark = new Laya.Sprite();
        mark.pivot(35, 35);
        mark.x = Utils.gridToStageX(this.row);
        mark.y = Utils.gridToStageY(this.col);
        Utils.addChild(mark, EditGlobal.main.game_top);
        this.mark = mark;
        this.on(Laya.Event.CLICK,this,this.onClick);
    }

    onClick(e){
        if(EditGlobal.select){
            var cfg = EditGlobal.config[EditGlobal.select];
            if(cfg){
                this.setData(cfg.type,cfg.color);
            }
        }
    }
    setColor(color) {
        if (color < 0 || color > 4) {
            if(color == 5){
                return this._setData('4',0);
            // }else if(color == 6){
            //     return this.setData(4,0);
            }
            console.log('unknow color:' + color);
            return;
        }
        // if (color == 0) {
        //     color = Utils.getInt(4) + 1;
        // }
        var name = "textures/jelly/" + this.assetNames[color] + ".png";
        this.loadImage(name);
    }

    setData(type, color) {
        
        this.type = type;
        this.color=color;
        this._setData(type,color);
    }
    _setData(type,color){
        type = ''+type;
        switch (type) {
            case '0':
                this.setColor(color);
                break;
            case '1':
                this.getImg(this, 'chocolate nuts');
                break;
            case '2':
                this.getImg(this, 'chocolate white');
                break;
            case '3':
                this.getImg(this, 'chocolate black');
                break;
            case '4':
                this.setColor(color);
                this.setMark('wall color');
                break;
            case '5':
                this.setColor(color);
                this.getImg(this.bgmark, 'cookie');
                break;
            case '6':
                this.setColor(color);
                this.getImg(this.bgmark, 'cookie honey');
                break;
            case '8':
                this.setColor(color);
                this.setMark('water');
                break;
            case '9':
                this.setColor(color);
                this.getImg(this.bgmark, 'cookie x3');
                break;
            case 'b':
                this.setColor(color);
                this.setMark('bush');
                break;
            default:
                console.log('unknow type:' + type);
                break;
        }
        

    }

    setMark(name) {
        this.getImg(this.mark, name);

    }

    getImg(sprite, name) {
        var imgName = "textures/jelly/" + name + ".png";
        sprite.loadImage(imgName)
    }

    getData(){
        return this.type+''+this.color;
    }

    setBlock(type) {
        var r = type == 1 ? 1 : type == 2 ? 2 : type == 3 ? 3 : 0;
        var isGem = type == 4;
        var s = type == 5;
        var iswater = type == 6;
        var ismask = type == 7;
        console.log("type:" + type);
        this.lastBlockSet = r;
        this.breakCountLeft = isGem ? 0 : r;
        this._isBlock = true;
        this.colorBlock = isGem ? Utils.getInt(4) : -1;
        this.isBombBlocked = this.isBombBlocked || s;
        if (isGem || iswater || ismask) {
            this.setColor(this.colorBlock);
            // this.setObject(GameConst.pool.getObject(this, Utils.gridToStageX(this.row), Utils.gridToStageY(this.col), GameConst.level.assetNumber, this.colorBlock));
        }
        if (iswater) {
            // this.isWaterBlock = true;
            // if (GameConst.level.isGenerated) {
            //     GameConst.level.objects.push(new SinglePlayObject(this.pos.x - 32, this.pos.y - 30, "water_splash",  GameConst.level.blockDestroyLayer, 10, 17, 1));
            //     SoundsManager.instance.playSound("water_set")
            // }
            // t.waterCells.push(this);
            // this.setTempBlock(true, Math.max(.5, this.tempBlockTime));
            // if (this.object && GameConst.level.isGenerated)
            //     this.object.playAnimation(117, 1, 1)
        }
        if (ismask) {
            this.isPlantBlock = true;
            this.plantBreaksLeft = 2;
            this.prevMarkAlpha = 1;
            var a = "bush root";
            var mark = this.mark ? this.mark : Utils.createBitmap(a);
            mark.pivotX = mark.pivotY = 30;
            mark.x = Utils.gridToStageX(this.row);
            mark.y = Utils.gridToStageX(this.col);
            if (mark.parent == null) {
                Utils.addChild(mark, EditGlobal.main.game_top);
            }
            this.mark = mark
        }
        var block = ismask ? "bush" : iswater ? "water" : s ? "mine_sprite_x" : isGem ? "wall color" : r == 0 ? "chocolate nuts" : r == 1 ? "chocolate white" : r == 2 ? "chocolate black" : "chocolate black x3 full";
        block = 'textures/jelly/' + block + ".png";
        console.log(block);
        this.loadImage(block);

    };

    // setMark(e) {
    //     if (typeof e === "undefined") {
    //         e = 1;
    //     }

    //     var name = e == 1 ? "cookie" : e == 2 ? "cookie honey" : "cookie x3";
    //     var mark = this.mark ? this.mark : Utils.createBitmap(name);
    //     mark.gotoAndStop(name);
    //     mark.x = Utils.gridToStageX(this.row);
    //     mark.y = Utils.gridToStageY(this.col);
    //     if (mark.parent == null)
    //         EditUI.instance.game_bg.addChild(mark);
    //     this.mark = mark;
    // };
}

